using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Net.Configuration;
using System.Xml.Linq;

namespace MB.TheBeerHouse.UI
{
    public partial class SearchPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bookListView.Visible = false;
                bookDetailsView.Visible = false;
                detailsLabel.Visible = false;
                detailsComplete.Visible = false;
            }
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            string query = searchKey.Text;
            Source1.SelectCommand = "SELECT ISBN, ProductID, Title, substring(Title,0,25) AS shortTitle, substring(Author,0,10) as shortAuthor, Author, UnitsInStock FROM [tbh_products] WHERE [ISBN] LIKE '%" + query + "%' OR [Title] LIKE '%" + query + "%' OR [Author] LIKE '%" + query + "%' ORDER BY [Author], [Title]";
            bookListView.DataBind();
            bookListView.Visible = true;
        }

        protected void bookListView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Select"))
            {
                int index = Convert.ToInt32(e.CommandArgument); // Get the index of the clicked row
                showBookDetails(index);
            }
        }

        private void showBookDetails(int rowIndex)
        {
            string primaryKey = (string)bookListView.DataKeys[rowIndex].Value;
            Source2.SelectCommand =
                "SELECT * FROM [tbh_products] WHERE ISBN = '" + primaryKey + "'";
            bookListView.DataBind();

            bookDetailsView.Visible = true;
            detailsLabel.Visible = true;
        }

        protected void bookDetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Request"))
                RequestBook();
        }

        private void RequestBook()
        {
            /* REQUESTS ARE NOT ALLOWED */
        }


    }
}